package com.stt.weather.design.scheduler;

import com.stt.weather.design.service.DesignWeatherService;
import com.stt.weather.util.CityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 天气数据定时任务调度器
 * 负责定期获取天气数据并发送通知
 */
@Component
public class DesignWeatherScheduler {

    /**
     * 天气服务对象
     */
    @Autowired
    private DesignWeatherService designWeatherService;

    /**
     * 定时获取天气数据并发送通知
     * 每小时执行一次（3600000毫秒）
     */
    @Scheduled(fixedRate = 3600000)
    public void fetchWeatherData() {

        // 遍历所有监控的城市
        for (String city : CityUtil.getCodeList()) {
            try {
                // 获取天气数据，观察者会自动收到通知
                designWeatherService.getWeatherData(city);
            } catch (Exception e) {
                System.err.println("获取城市 " + city + " 的天气数据失败: " + e.getMessage());
            }
        }
    }
} 